<?php

/**
 * 签约业绩仓库
 * @author liuyw01 2016-1-13
 */
namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\repositories\common\MyscrmContractSyncDeleteDataRepository;
use common\support\StringHelper;
use common\entities\myhtsq\SignDelyEntity;

class SignDelyRepository extends BaseRepository {

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = []) {
        parent::__construct($config);
    }

    /**
     * 根据合同id,获取合同业绩列表
     * @param string $contractId 合同id
     * @return array
     */
    public static function getSignDelyListByContractId($contractId) {
        return SignDelyEntity::find()->where(['contract_id' => $contractId])->asArray()->all();
    }

    /**
     * 根据交付id,删除交付业绩
     * @param string $dely_id 交付id
     * @return boolean
     */
    public static function deleteSignDelyByDelyId($dely_id)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteResult = SignDelyEntity::deleteAll(['dely_id' => $dely_id]);

        if ($deleteResult > 0) {
            $contractSyncDeleteDataRepository->add('myscrm_sign_dely', 'dely_id', $dely_id);
        }

        return $deleteResult;
    }

    /**
     * 新增签约业绩
     * @param SignDelyEntity $signDelyModel 签约业绩模型
     * @return boolean|string
     */
    public static function addSignDely($signDelyModel) {
        $signDelyEntity = new SignDelyEntity ();
        $data = $signDelyModel->attributes;
        $signDelyEntity->load($data, '');
        if ($signDelyEntity->save()) {
            return $signDelyEntity->dely_id;
        } else {
            return false;
        }
    }

    /**
     * 更新签约业绩
     * @param $signDelyModel 签约业绩模型
     * @return string|boolean
     */
    public static function updateSignDely($signDelyModel) {
        $signDelyEntity = new SignDelyEntity ();
        $signDelyModelInfo = SignDelyEntity::find()->where(['dely_id' => $signDelyModel->dely_id])->one();
        $data = $signDelyModel->attributes;
        $signDelyModelInfo->load($data, '');
        if ($signDelyModelInfo->save()) {
            return $signDelyEntity->dely_id;
        } else {
            return false;
        }
    }

    /**
     * 根据合同id,获取合同的签约业绩总额
     * @param string $contractId 合同id
     * @return float
     */
    public static function getSignAchievementSumByContractId($contractId) {
        return SignDelyEntity::find()->where(['contract_id' => $contractId])->sum('achievement_amount');
    }

    /**
     * 删除交付业绩
     * @param string $paymentId 回款ID
     * @return boolean|int
     */
    public static function deleteSignDelyByPaymentId($paymentId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteData = SignDelyEntity::findAll(['payment_id' => $paymentId]);

        $deleteResult = SignDelyEntity::deleteAll(['payment_id' => $paymentId]);

        if ($deleteResult > 0) {
            foreach ($deleteData as $data) {
                $contractSyncDeleteDataRepository->add('myscrm_sign_dely', 'dely_id', $data['dely_id']);
            }
        }

        return $deleteResult;
    }
    
}
